/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrs.irobot.baloghkoys;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultCaret;

/**
 *
 * @author baloghsz
 */
public class GUI extends javax.swing.JFrame implements KeyListener {

    
    private iRobotImage robotImage = new iRobotImage();
    private LowLevelDrv lowLevelDrv = null;
    private Thread goWaypointsThread;
    private ArrayList<Waypoint> waypoints = new ArrayList();
        
    private Waypoint lastGotoWaypoint = new Waypoint(0,0,0);
    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
        // scrollovanie logovacieho okna
        DefaultCaret caret = (DefaultCaret)this.logTextArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        
        // Thread for repainting time and iRobot sensors animation
        Thread refreshTime;
        refreshTime = new Thread(new Runnable() {
         @Override
         public void run() {
             DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             while( true ){     
                 mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.Sleep(250);
                 Date date = new Date();
                 timeLabel.setText( dateFormat.format(date) );
                 actualXpositionLabel.setText(String.format("%.1f mm",lowLevelDrv.sensors.get_x_position()));
                 actualYpositionLabel.setText(String.format("%.1f mm",lowLevelDrv.sensors.get_y_position()));
                 actualSpeedLabel.setText(String.format("%d mm/s",lowLevelDrv.sensors.requested_velocity()));
                 actualOrientationLabel.setText(String.format("%d Degrees",lowLevelDrv.sensors.angle()));
                 drivenDistanceLabel.setText( String.format("%d mm",lowLevelDrv.sensors.distance()));
             }
         }
        });
        refreshTime.start();  
        this.setFocusable(true);        
        this.jInternalFrame1.setFocusable(true);
        this.jInternalFrame2.setFocusable(true);
        this.jInternalFrame3.setFocusable(true);
        this.jTabbedPane1.setFocusable(true);
       
        this.jInternalFrame1.addKeyListener(this);
        this.jInternalFrame2.addKeyListener(this);
        this.jInternalFrame3.addKeyListener(this);
        this.jTabbedPane1.addKeyListener(this);
       
        this.addKeyListener(this);
    }
    
    public void setRobotImage(iRobotImage robotImage) {
        this.robotImage = robotImage;
    }
    
    public void setLowLevelDrv(LowLevelDrv lowLevelDrv) {
        this.lowLevelDrv = lowLevelDrv;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        buttonForwardLowLevel = new javax.swing.JButton();
        buttonStopLowLevel = new javax.swing.JButton();
        buttonBackwardLowLevel = new javax.swing.JButton();
        sliderSpeedLowLevel = new javax.swing.JSlider();
        jLabel9 = new javax.swing.JLabel();
        buttonTurnRightAngleLowLevel = new javax.swing.JButton();
        buttonTurnLeftAngleLowLevel = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        buttonForward = new javax.swing.JButton();
        buttonStopManual = new javax.swing.JButton();
        buttonBackward = new javax.swing.JButton();
        buttonLeft = new javax.swing.JButton();
        buttonRight = new javax.swing.JButton();
        buttonRotate = new javax.swing.JButton();
        buttonTurnBack = new javax.swing.JButton();
        sliderSpeedManual = new javax.swing.JSlider();
        sliderAngle = new javax.swing.JSlider();
        buttonTurnRightAngle = new javax.swing.JButton();
        buttonTurnLeftAngle = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        sliderDistance = new javax.swing.JSlider();
        jPanel4 = new javax.swing.JPanel();
        toPointRadio = new javax.swing.JRadioButton();
        fromFileRadio = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        xTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        yTextField = new javax.swing.JTextField();
        goPointButton = new javax.swing.JButton();
        buttonStopAutomatic = new javax.swing.JButton();
        chooseFileButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        fileNameTextArea = new javax.swing.JTextArea();
        goFileButton = new javax.swing.JButton();
        sliderSpeedAutomatic = new javax.swing.JSlider();
        jLabel5 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nextWaypointLabel = new javax.swing.JLabel();
        saveWaypointsButton = new javax.swing.JButton();
        replayFileButton = new javax.swing.JButton();
        buttonStopAutomatic1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        sliderSpeedVacuum = new javax.swing.JSlider();
        vacuumButton = new javax.swing.JButton();
        buttonStopVacuuming = new javax.swing.JButton();
        vacuumSqareRadio = new javax.swing.JRadioButton();
        vacuumSquareSide = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        viewPanel = new javax.swing.JPanel(){         
            public void paint(Graphics g){        
                if( robotImage != null )
                g.drawImage( robotImage.getImage(), 0, 0, null);   
                else
                super.paint(g);
            } 
        };
        jInternalFrame3 = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        logTextArea = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jInternalFrame2 = new javax.swing.JInternalFrame();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        actualXpositionLabel = new javax.swing.JLabel();
        actualYpositionLabel = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        actualSpeedLabel = new javax.swing.JLabel();
        resetPositionButton = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        actualOrientationLabel = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        drivenDistanceLabel = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("iRobot");
        setBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });

        jTabbedPane1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N

        buttonForwardLowLevel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_up.JPG"))); // NOI18N
        buttonForwardLowLevel.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonForwardLowLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonForwardLowLevelActionPerformed(evt);
            }
        });

        buttonStopLowLevel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stop.JPG"))); // NOI18N
        buttonStopLowLevel.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonStopLowLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopLowLevelActionPerformed(evt);
            }
        });

        buttonBackwardLowLevel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_down.JPG"))); // NOI18N
        buttonBackwardLowLevel.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonBackwardLowLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBackwardLowLevelActionPerformed(evt);
            }
        });

        sliderSpeedLowLevel.setMajorTickSpacing(100);
        sliderSpeedLowLevel.setMaximum(500);
        sliderSpeedLowLevel.setMinorTickSpacing(50);
        sliderSpeedLowLevel.setPaintLabels(true);
        sliderSpeedLowLevel.setPaintTicks(true);
        sliderSpeedLowLevel.setValue(200);
        sliderSpeedLowLevel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderSpeedLowLevelStateChanged(evt);
            }
        });

        jLabel9.setText("Speed");

        buttonTurnRightAngleLowLevel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_turn.jpg"))); // NOI18N
        buttonTurnRightAngleLowLevel.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonTurnRightAngleLowLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnRightAngleLowLevelActionPerformed(evt);
            }
        });

        buttonTurnLeftAngleLowLevel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_turn_2.JPG"))); // NOI18N
        buttonTurnLeftAngleLowLevel.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonTurnLeftAngleLowLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnLeftAngleLowLevelActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel10.setText("[mm/h]");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(buttonTurnLeftAngleLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buttonBackwardLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(buttonStopLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonTurnRightAngleLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(buttonForwardLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sliderSpeedLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10))
                    .addComponent(sliderSpeedLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(buttonForwardLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonTurnLeftAngleLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonStopLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTurnRightAngleLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonBackwardLowLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(136, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("LowLevel", jPanel5);

        buttonForward.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_up.JPG"))); // NOI18N
        buttonForward.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonForwardActionPerformed(evt);
            }
        });

        buttonStopManual.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stop.JPG"))); // NOI18N
        buttonStopManual.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonStopManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopManualActionPerformed(evt);
            }
        });

        buttonBackward.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_down.JPG"))); // NOI18N
        buttonBackward.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonBackward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBackwardActionPerformed(evt);
            }
        });

        buttonLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_left.JPG"))); // NOI18N
        buttonLeft.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLeftActionPerformed(evt);
            }
        });

        buttonRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_right.JPG"))); // NOI18N
        buttonRight.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRightActionPerformed(evt);
            }
        });

        buttonRotate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_round.JPG"))); // NOI18N
        buttonRotate.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonRotate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRotateActionPerformed(evt);
            }
        });

        buttonTurnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_back.JPG"))); // NOI18N
        buttonTurnBack.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonTurnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnBackActionPerformed(evt);
            }
        });

        sliderSpeedManual.setMajorTickSpacing(100);
        sliderSpeedManual.setMaximum(500);
        sliderSpeedManual.setMinorTickSpacing(50);
        sliderSpeedManual.setPaintLabels(true);
        sliderSpeedManual.setPaintTicks(true);
        sliderSpeedManual.setSnapToTicks(true);
        sliderSpeedManual.setValue(200);
        sliderSpeedManual.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderSpeedManualStateChanged(evt);
            }
        });

        sliderAngle.setMajorTickSpacing(45);
        sliderAngle.setMaximum(360);
        sliderAngle.setMinorTickSpacing(45);
        sliderAngle.setPaintLabels(true);
        sliderAngle.setPaintTicks(true);
        sliderAngle.setValue(45);

        buttonTurnRightAngle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_turn.jpg"))); // NOI18N
        buttonTurnRightAngle.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonTurnRightAngle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnRightAngleActionPerformed(evt);
            }
        });

        buttonTurnLeftAngle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_turn_2.JPG"))); // NOI18N
        buttonTurnLeftAngle.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonTurnLeftAngle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTurnLeftAngleActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel11.setText("[mm/h]");

        jLabel13.setText("Speed");

        jLabel15.setText("Angle [Degrees]");

        jLabel16.setText("Distance [cm]");

        sliderDistance.setMajorTickSpacing(100);
        sliderDistance.setMaximum(500);
        sliderDistance.setMinorTickSpacing(25);
        sliderDistance.setPaintLabels(true);
        sliderDistance.setPaintTicks(true);
        sliderDistance.setSnapToTicks(true);
        sliderDistance.setToolTipText("");
        sliderDistance.setValue(100);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonRotate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonTurnLeftAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(buttonBackward, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonTurnRightAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(buttonStopManual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(buttonForward, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonTurnBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sliderSpeedManual, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sliderAngle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(sliderDistance, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(11, 11, 11))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11))
                    .addComponent(sliderSpeedManual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonForward, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRotate, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTurnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonStopManual, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonBackward, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTurnLeftAngle, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTurnRightAngle, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sliderAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel16)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sliderDistance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Manual", jPanel3);

        toPointRadio.setText("Navigation to a point");
        toPointRadio.setFocusPainted(false);
        toPointRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toPointRadioActionPerformed(evt);
            }
        });

        fromFileRadio.setSelected(true);
        fromFileRadio.setText("Navigation from a file");
        fromFileRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromFileRadioActionPerformed(evt);
            }
        });

        jLabel3.setText("x:");

        xTextField.setText("0");
        xTextField.setEnabled(toPointRadio.isSelected());
        xTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xTextFieldActionPerformed(evt);
            }
        });

        jLabel4.setText("y:");

        yTextField.setText("0");
        yTextField.setEnabled(toPointRadio.isSelected());
        yTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yTextFieldActionPerformed(evt);
            }
        });

        goPointButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/arrow_go.jpg"))); // NOI18N
        goPointButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        goPointButton.setEnabled(toPointRadio.isSelected());
        goPointButton.setName(""); // NOI18N
        goPointButton.setPreferredSize(new java.awt.Dimension(48, 48));
        goPointButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goPointButtonActionPerformed(evt);
            }
        });

        buttonStopAutomatic.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stop_small.jpg"))); // NOI18N
        buttonStopAutomatic.setEnabled(fromFileRadio.isSelected());
        buttonStopAutomatic.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonStopAutomatic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopAutomaticActionPerformed(evt);
            }
        });

        chooseFileButton.setText("Choose a file");
        chooseFileButton.setEnabled(fromFileRadio.isSelected());
        chooseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        fileNameTextArea.setColumns(20);
        fileNameTextArea.setRows(5);
        fileNameTextArea.setText("C:\\Users\\Szabi\\Documents\\waypoints_stvorec_1m.txt");
        fileNameTextArea.setEnabled(fromFileRadio.isSelected());
        jScrollPane2.setViewportView(fileNameTextArea);

        goFileButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Play.gif"))); // NOI18N
        goFileButton.setEnabled(fromFileRadio.isSelected());
        goFileButton.setPreferredSize(new java.awt.Dimension(48, 48));
        goFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goFileButtonActionPerformed(evt);
            }
        });

        sliderSpeedAutomatic.setMajorTickSpacing(100);
        sliderSpeedAutomatic.setMaximum(500);
        sliderSpeedAutomatic.setMinorTickSpacing(50);
        sliderSpeedAutomatic.setPaintLabels(true);
        sliderSpeedAutomatic.setPaintTicks(true);
        sliderSpeedAutomatic.setSnapToTicks(true);
        sliderSpeedAutomatic.setToolTipText("");
        sliderSpeedAutomatic.setValue(200);
        sliderSpeedAutomatic.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderSpeedAutomaticStateChanged(evt);
            }
        });

        jLabel5.setText("Speed");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel12.setText("[mm/h]");

        jLabel2.setText("Next waypoint:");

        nextWaypointLabel.setText("no waypoint loaded");

        saveWaypointsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Save.gif"))); // NOI18N
        saveWaypointsButton.setEnabled(toPointRadio.isSelected());
        saveWaypointsButton.setPreferredSize(new java.awt.Dimension(48, 48));
        saveWaypointsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveWaypointsButtonActionPerformed(evt);
            }
        });

        replayFileButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Replay.gif"))); // NOI18N
        replayFileButton.setEnabled(fromFileRadio.isSelected());
        replayFileButton.setPreferredSize(new java.awt.Dimension(48, 48));
        replayFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replayFileButtonActionPerformed(evt);
            }
        });

        buttonStopAutomatic1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stop_small.jpg"))); // NOI18N
        buttonStopAutomatic1.setEnabled(toPointRadio.isSelected());
        buttonStopAutomatic1.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonStopAutomatic1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopAutomatic1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(fromFileRadio)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(xTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(yTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(100, 100, 100))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(toPointRadio)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nextWaypointLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2)
                                    .addComponent(chooseFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel4Layout.createSequentialGroup()
                                        .addComponent(replayFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(goFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(buttonStopAutomatic, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(saveWaypointsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(goPointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonStopAutomatic1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(sliderSpeedAutomatic, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(81, 81, 81))))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(toPointRadio)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(yTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17)
                        .addComponent(sliderSpeedAutomatic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(goPointButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveWaypointsButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonStopAutomatic1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(fromFileRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chooseFileButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(11, 11, 11)
                .addComponent(nextWaypointLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(goFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(replayFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonStopAutomatic, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(64, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Automatic", jPanel4);

        jLabel18.setText("Speed");

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel19.setText("[mm/h]");

        sliderSpeedVacuum.setMajorTickSpacing(100);
        sliderSpeedVacuum.setMaximum(500);
        sliderSpeedVacuum.setMinorTickSpacing(50);
        sliderSpeedVacuum.setPaintLabels(true);
        sliderSpeedVacuum.setPaintTicks(true);
        sliderSpeedVacuum.setValue(200);
        sliderSpeedVacuum.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderSpeedVacuumStateChanged(evt);
            }
        });

        vacuumButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Play.gif"))); // NOI18N
        vacuumButton.setEnabled(fromFileRadio.isSelected());
        vacuumButton.setPreferredSize(new java.awt.Dimension(48, 48));
        vacuumButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vacuumButtonActionPerformed(evt);
            }
        });

        buttonStopVacuuming.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stop_small.jpg"))); // NOI18N
        buttonStopVacuuming.setEnabled(fromFileRadio.isSelected());
        buttonStopVacuuming.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonStopVacuuming.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopVacuumingActionPerformed(evt);
            }
        });

        vacuumSqareRadio.setSelected(true);
        vacuumSqareRadio.setText("Vacuum square");

        vacuumSquareSide.setText("1000");
        vacuumSquareSide.setEnabled(vacuumSqareRadio.isSelected());
        vacuumSquareSide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vacuumSquareSideActionPerformed(evt);
            }
        });

        jLabel20.setText("mm");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sliderSpeedVacuum, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(buttonStopVacuuming, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vacuumButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 15, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(vacuumSqareRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vacuumSquareSide, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel20)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel19))
                    .addComponent(sliderSpeedVacuum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vacuumSqareRadio)
                    .addComponent(vacuumSquareSide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vacuumButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonStopVacuuming, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 301, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Vacuum", jPanel1);

        jTabbedPane1.setSelectedIndex(2);

        jInternalFrame1.setTitle("View");
        jInternalFrame1.setPreferredSize(new java.awt.Dimension(300, 335));
        jInternalFrame1.setVisible(true);

        viewPanel.setBackground(new java.awt.Color(255, 255, 255));
        viewPanel.setToolTipText("");
        viewPanel.setPreferredSize(new java.awt.Dimension(300, 300));

        javax.swing.GroupLayout viewPanelLayout = new javax.swing.GroupLayout(viewPanel);
        viewPanel.setLayout(viewPanelLayout);
        viewPanelLayout.setHorizontalGroup(
            viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 284, Short.MAX_VALUE)
        );
        viewPanelLayout.setVerticalGroup(
            viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 299, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(viewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(viewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jInternalFrame3.setTitle("Log");
        jInternalFrame3.setVisible(true);

        logTextArea.setEditable(false);
        logTextArea.setColumns(20);
        logTextArea.setRows(5);
        logTextArea.setPreferredSize(null);
        jScrollPane1.setViewportView(logTextArea);

        javax.swing.GroupLayout jInternalFrame3Layout = new javax.swing.GroupLayout(jInternalFrame3.getContentPane());
        jInternalFrame3.getContentPane().setLayout(jInternalFrame3Layout);
        jInternalFrame3Layout.setHorizontalGroup(
            jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jInternalFrame3Layout.setVerticalGroup(
            jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
        );

        jLabel1.setText("Time:");

        jInternalFrame2.setTitle("Status");
        jInternalFrame2.setMaximumSize(new java.awt.Dimension(233, 339));
        jInternalFrame2.setVisible(true);

        jLabel6.setText("Actual position x:");

        jLabel7.setText("Actual position y:");

        jLabel8.setText("Actual orientation:");

        resetPositionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/X_icon.jpg"))); // NOI18N
        resetPositionButton.setEnabled(true);
        resetPositionButton.setPreferredSize(new java.awt.Dimension(48, 48));
        resetPositionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetPositionButtonActionPerformed(evt);
            }
        });

        jLabel14.setText("Actual speed      :");

        jLabel17.setText("Driven distance :");

        javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
        jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
        jInternalFrame2Layout.setHorizontalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addGap(26, 26, 26)
                        .addComponent(drivenDistanceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel14))
                        .addGap(18, 18, 18)
                        .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(actualSpeedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                                .addComponent(actualOrientationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(resetPositionButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addGap(24, 24, 24)
                                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(actualXpositionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(actualYpositionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jInternalFrame2Layout.setVerticalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jInternalFrame2Layout.createSequentialGroup()
                        .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jInternalFrame2Layout.createSequentialGroup()
                                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(actualXpositionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(actualYpositionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8))
                            .addComponent(actualOrientationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14))
                    .addComponent(actualSpeedLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(drivenDistanceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(resetPositionButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        timeLabel.setText("actualTime");

        fileMenu.setMnemonic('f');
        fileMenu.setText("File");

        openMenuItem.setMnemonic('o');
        openMenuItem.setText("Open");
        fileMenu.add(openMenuItem);

        saveMenuItem.setMnemonic('s');
        saveMenuItem.setText("Save");
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setMnemonic('a');
        saveAsMenuItem.setText("Save As ...");
        saveAsMenuItem.setDisplayedMnemonicIndex(5);
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setMnemonic('e');
        editMenu.setText("Edit");

        cutMenuItem.setMnemonic('t');
        cutMenuItem.setText("Cut");
        editMenu.add(cutMenuItem);

        copyMenuItem.setMnemonic('y');
        copyMenuItem.setText("Copy");
        editMenu.add(copyMenuItem);

        pasteMenuItem.setMnemonic('p');
        pasteMenuItem.setText("Paste");
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setMnemonic('d');
        deleteMenuItem.setText("Delete");
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("Help");

        contentsMenuItem.setMnemonic('c');
        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jInternalFrame2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jInternalFrame3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jInternalFrame1, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                            .addComponent(jInternalFrame2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jInternalFrame3))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 1, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(timeLabel)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        Logger.log("Application terminated by user"); 
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void buttonTurnLeftAngleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnLeftAngleActionPerformed
        int angle = this.sliderAngle.getValue();
        int speed    = this.sliderSpeedManual.getValue();
        Logger.log("Manual control: Rotate left "+angle+"Deg by "+speed+"mm/h"); 
        this.lowLevelDrv.turn(speed, -angle);
    }//GEN-LAST:event_buttonTurnLeftAngleActionPerformed

    private void buttonRotateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRotateActionPerformed
        int speed    = this.sliderSpeedManual.getValue();       
        Logger.log("Manual control: Rotate left 360Deg by "+speed+"mm/s"); 
        this.lowLevelDrv.turn( speed, -360 ); 
    }//GEN-LAST:event_buttonRotateActionPerformed

    private void buttonTurnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnBackActionPerformed
        int speed    = this.sliderSpeedManual.getValue();       
        Logger.log("Manual control: Rotate right 180Deg by "+speed+"mm/s"); 
        this.lowLevelDrv.turn( speed, 180 ); 
    }//GEN-LAST:event_buttonTurnBackActionPerformed

    private void buttonRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRightActionPerformed
        Logger.log("Manual control: Rotate right 90Deg"); 
        int speed    = this.sliderSpeedManual.getValue();
        this.lowLevelDrv.turn(speed, 90);
    }//GEN-LAST:event_buttonRightActionPerformed

    private void buttonStopManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopManualActionPerformed
        goWaypointsThread.interrupt();// este neviem ci to bude dobre
        goWaypointsThread.stop();
        this.lowLevelDrv.stop();
        Logger.log("Manual control: STOP"); 
    }//GEN-LAST:event_buttonStopManualActionPerformed

    private void buttonLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLeftActionPerformed
        Logger.log("Manual control: Rotate left 90Deg"); 
        int speed    = this.sliderSpeedManual.getValue();
        this.lowLevelDrv.turn(speed, -90);
    }//GEN-LAST:event_buttonLeftActionPerformed

    private void buttonForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonForwardActionPerformed
        int distance = this.sliderDistance.getValue(); // [cm]
        int speed    = this.sliderSpeedManual.getValue();
        Logger.log("Manual control: Go forward "+distance+"cm by "+speed+"mm/s"); 
        this.lowLevelDrv.go_forward( speed, distance*10 ); // distance is in mm
    }//GEN-LAST:event_buttonForwardActionPerformed

    private void buttonBackwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBackwardActionPerformed
        int distance = this.sliderDistance.getValue();  // [cm]
        int speed    = this.sliderSpeedManual.getValue();
        Logger.log("Manual control: Go backward "+distance+"cm by "+speed+"mm/s"); 
        this.lowLevelDrv.go_forward( speed, -distance*10 ); // distance is in mm
    }//GEN-LAST:event_buttonBackwardActionPerformed

    private void buttonTurnRightAngleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnRightAngleActionPerformed
        int angle = this.sliderAngle.getValue();
        int speed    = this.sliderSpeedManual.getValue();
        Logger.log("Manual control: Rotate right "+angle+"Deg by "+speed+"mm/h"); 
        this.lowLevelDrv.turn(speed, angle); 
    }//GEN-LAST:event_buttonTurnRightAngleActionPerformed

    private void sliderSpeedManualStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderSpeedManualStateChanged
        Logger.log("Manual control: Speed set to "+this.sliderSpeedManual.getValue(),1 ); 
    }//GEN-LAST:event_sliderSpeedManualStateChanged

    private void checkboxes(){
        this.xTextField.setEnabled(this.toPointRadio.isSelected());
        this.yTextField.setEnabled(this.toPointRadio.isSelected());
        this.goPointButton.setEnabled(this.toPointRadio.isSelected());
        this.saveWaypointsButton.setEnabled(this.toPointRadio.isSelected());
        this.buttonStopAutomatic1.setEnabled(this.toPointRadio.isSelected());
        
        this.fileNameTextArea.setEnabled(this.fromFileRadio.isSelected());
        this.goFileButton.setEnabled(this.fromFileRadio.isSelected());
        this.replayFileButton.setEnabled(this.fromFileRadio.isSelected());
        this.chooseFileButton.setEnabled(this.fromFileRadio.isSelected());
        this.buttonStopAutomatic.setEnabled(this.fromFileRadio.isSelected());
    }
    
    private void toPointRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toPointRadioActionPerformed
       this.fromFileRadio.setSelected(!this.toPointRadio.isSelected());
        checkboxes();
    }//GEN-LAST:event_toPointRadioActionPerformed

    private void fromFileRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromFileRadioActionPerformed
        this.toPointRadio.setSelected(!this.fromFileRadio.isSelected());
        checkboxes();
    }//GEN-LAST:event_fromFileRadioActionPerformed

    private void goPointButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goPointButtonActionPerformed
        
        if( goWaypointsThread==null || !goWaypointsThread.isAlive() ){
             Logger.log("Starting thread for going to point",5);
             goWaypointsThread = new Thread(new Runnable() {
                @Override
                public void run() {
                        int x=0,y=0;                                    
                        try{
                            x = Integer.parseInt(xTextField.getText());
                        }catch( Exception e ){           
                            JOptionPane.showMessageDialog(null, "Unable to parse X parameter: \""+xTextField.getText()+"\"", "Error", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        try{
                            y = Integer.parseInt(yTextField.getText());
                        }catch( Exception e ){           
                            JOptionPane.showMessageDialog(null, "Unable to parse Y parameter: \""+yTextField.getText()+"\"", "Error", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        int speed = sliderSpeedAutomatic.getValue();
                        Waypoint wpt = new Waypoint(x,y,speed,null);
                        Logger.log("Going to waypoint: "+wpt.toString());
                        waypoints.add(wpt);
                        mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( wpt.toString() );                                                 
                        goto_waypoint(wpt);
                        mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( "finished" );                      
                    }
                }
            );
            goWaypointsThread.start();  
        }else{
            Logger.log("Following waypoints already! Try later");
        }
        
    }//GEN-LAST:event_goPointButtonActionPerformed

    private void yTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yTextFieldActionPerformed
       
    }//GEN-LAST:event_yTextFieldActionPerformed

    private void buttonStopAutomaticActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopAutomaticActionPerformed
        if( goWaypointsThread != null ){
            goWaypointsThread.interrupt();// este neviem ci to bude dobre
            goWaypointsThread.stop();
        }
        this.lowLevelDrv.stop();        
        Logger.log("Automatic control: Stop"); 
    }//GEN-LAST:event_buttonStopAutomaticActionPerformed

    private void goFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goFileButtonActionPerformed

        if( goWaypointsThread==null || !goWaypointsThread.isAlive() ){
             Logger.log("Starting thread for following waypoints from file",5);
             goWaypointsThread = new Thread(new Runnable() {
                @Override
                public void run() {
                       //waypoints = new ArrayList();
                       String fileName = mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.fileNameTextArea.getText();
                       WaypointFileParser waypointFileParser = new WaypointFileParser( fileName );
                       Iterator iterator_waypoints = waypointFileParser.iterator();
                       MapGUI mapGUI = new MapGUI(true,false);
                       mapGUI.setVisible(true);
                       mapGUI.mapImage = new MapImage(true);
                       lastGotoWaypoint=new Waypoint(0,0,0);
                       int waypoints_index = waypoints.size();
                       while( iterator_waypoints.hasNext() ){
                           Waypoint wpt = (Waypoint)iterator_waypoints.next();
                           while( !wpt.isOrdinary() && iterator_waypoints.hasNext() )
                               wpt = (Waypoint)iterator_waypoints.next();
                           if( !wpt.isOrdinary() )
                               break;
                           mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( wpt.toString() );  
                           //mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.Sleep(1000);
                           mapGUI.mapImage.addWaypoint(wpt);
                           mapGUI.repaint();     
                           goto_waypoint(wpt);
                           while( waypoints_index < waypoints.size() ){
                               mapGUI.mapImage.addWaypoint( waypoints.get(waypoints_index++) );
                               mapGUI.repaint();     
                           }
                           
                           
                       }
                       mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( "finished" );                      
                    }
                }
            );
            goWaypointsThread.start();  
        }else{
            Logger.log("Following waypoints already! Try later");
        }
        
    }//GEN-LAST:event_goFileButtonActionPerformed

    private void sliderSpeedAutomaticStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderSpeedAutomaticStateChanged
        Logger.log("Automatic control: Speed set to "+this.sliderSpeedAutomatic.getValue(),1 );        
    }//GEN-LAST:event_sliderSpeedAutomaticStateChanged

    private void chooseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileButtonActionPerformed
        JFileChooser jfc = new JFileChooser(); 
        if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
            this.fileNameTextArea.setText(jfc.getSelectedFile().getPath());
        } 
    }//GEN-LAST:event_chooseFileButtonActionPerformed

    private void buttonForwardLowLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonForwardLowLevelActionPerformed
        this.lowLevelDrv.go_forward( this.sliderSpeedLowLevel.getValue() );
        Logger.log("Low level control: Go forward at "+this.sliderSpeedLowLevel.getValue()+"mm/h"); 
    }//GEN-LAST:event_buttonForwardLowLevelActionPerformed

    private void buttonStopLowLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopLowLevelActionPerformed
        this.lowLevelDrv.stop();     
        Logger.log("Low level control: Stop"); 
    }//GEN-LAST:event_buttonStopLowLevelActionPerformed

    private void buttonBackwardLowLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBackwardLowLevelActionPerformed
        this.lowLevelDrv.go_backward( this.sliderSpeedLowLevel.getValue() );        
        Logger.log("Low level control: Go backward at "+this.sliderSpeedLowLevel.getValue()+"mm/h"); 
    }//GEN-LAST:event_buttonBackwardLowLevelActionPerformed

    private void sliderSpeedLowLevelStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderSpeedLowLevelStateChanged
        Logger.log("LowLevel control: Speed set to "+this.sliderSpeedLowLevel.getValue(),1 ); 
    }//GEN-LAST:event_sliderSpeedLowLevelStateChanged

    private void buttonTurnRightAngleLowLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnRightAngleLowLevelActionPerformed
        this.lowLevelDrv.turn_clockwise( this.sliderSpeedLowLevel.getValue() );
        Logger.log("Low level control: Turn right at "+this.sliderSpeedLowLevel.getValue()+"mm/h"); 
    }//GEN-LAST:event_buttonTurnRightAngleLowLevelActionPerformed

    private void buttonTurnLeftAngleLowLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTurnLeftAngleLowLevelActionPerformed
        this.lowLevelDrv.turn_counterclockwise( this.sliderSpeedLowLevel.getValue() );
        Logger.log("Low level control: Turn left at "+this.sliderSpeedLowLevel.getValue()+"mm/h"); 
    }//GEN-LAST:event_buttonTurnLeftAngleLowLevelActionPerformed

    private void replayFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replayFileButtonActionPerformed
        
        Logger.log("Starting thread for reaplaying waypoints from file",5);
        Thread replayWaypointsThread = new Thread(new Runnable() {
           @Override
           public void run() {
                  String fileName = mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.fileNameTextArea.getText();
                  WaypointFileParser waypointFileParser = new WaypointFileParser( fileName );
                  Iterator waypoints = waypointFileParser.iterator();
                  MapGUI mapGUI = new MapGUI(false,true);
                  mapGUI.setVisible(true);
                  mapGUI.mapImage = new MapImage(true);
                  while( waypoints.hasNext() ){
                      Waypoint wpt = (Waypoint)waypoints.next();
                      //mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( wpt.toString() );  
                      //wait to iRobot reach waypoint
                      mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.Sleep(mapGUI.replaySlider.getValue()*100);
                      mapGUI.mapImage.addWaypoint(wpt);
                      mapGUI.repaint();         
                  }
                  mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( "finished" );                      
               }
           }
       );
       replayWaypointsThread.start();  
    }//GEN-LAST:event_replayFileButtonActionPerformed

    private void xTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xTextFieldActionPerformed
        
    }//GEN-LAST:event_xTextFieldActionPerformed

     
    /**
     * Saves all waypoints from this image to file
     * @param fileName name of file to write waypoints
     */
    public void saveToFile( String fileName ){
        Logger.log("Saving waypoints to file: "+fileName,3);
        String textToFile = "";
        Iterator i = waypoints.iterator();
        while( i.hasNext() ){
            Waypoint w = (Waypoint) i.next();
            textToFile = textToFile.concat(w.toFile());
        }
        saveStringToFile( fileName, textToFile );
    }
    
        /**
     * Writes text to file
     * @param fileName name of file to save text
     * @param text text to be written to file
     */
    private void saveStringToFile( String fileName, String text ){
        try
        {
            FileWriter fw = new FileWriter( fileName );
            fw.write(text);
            fw.close();            
            Logger.log("Waypoints saved to file: "+fileName+" successfully",3);
        }
        catch (Exception e)
        {
            Logger.log("Failed to save to file. Exception: "+e.toString());
            JOptionPane.showMessageDialog(null, "Failed to save to file. Exception: "+e.toString(),"Error", JOptionPane.ERROR_MESSAGE);            
        }
    }
    
    private void saveWaypointsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveWaypointsButtonActionPerformed
        JFileChooser jfc = new JFileChooser(); 
        if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
            saveToFile( jfc.getSelectedFile().getPath() );
            waypoints = new ArrayList();
        }else{
            //TODO sanitize
        } 
    }//GEN-LAST:event_saveWaypointsButtonActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        this.keyPressed(evt);
    }//GEN-LAST:event_formKeyPressed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
        this.keyReleased(evt);
    }//GEN-LAST:event_formKeyReleased

    private void resetPositionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetPositionButtonActionPerformed
        lowLevelDrv.stop();
        MrsIrobotBaloghkoys.Sleep(1000);
        //lowLevelDrv.init();
        MrsIrobotBaloghkoys.Sleep(1000);
        lowLevelDrv.sensors.query();
        lowLevelDrv.sensors.reset_angle();
        lowLevelDrv.sensors.reset_distance();
        lowLevelDrv.sensors.reset_xy();
        Logger.log("Actual data reseted to zeros");
    }//GEN-LAST:event_resetPositionButtonActionPerformed

    private void buttonStopAutomatic1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopAutomatic1ActionPerformed
        goWaypointsThread.interrupt();// este neviem ci to bude dobre
        goWaypointsThread.stop();        
        this.lowLevelDrv.stop();  //TODO to asi nebude fungovat, lebo robot bude chciet pokracovat v goto_waypoint()
        Logger.log("Automatic control: Stop"); 

    }//GEN-LAST:event_buttonStopAutomatic1ActionPerformed

    private void sliderSpeedVacuumStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderSpeedVacuumStateChanged
        Logger.log("Vacuum control: Speed set to "+this.sliderSpeedVacuum.getValue(),1 );        
    }//GEN-LAST:event_sliderSpeedVacuumStateChanged

    private void vacuumButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vacuumButtonActionPerformed

        if( goWaypointsThread==null || !goWaypointsThread.isAlive() ){
             Logger.log("Starting thread for vacuuming waypoints",5);
             goWaypointsThread = new Thread(new Runnable() {                 
                @Override
                public void run() {
                       //waypoints = new ArrayList();
                       int speed = sliderSpeedVacuum.getValue();
                       int square_side;
                       try{
                           square_side = Integer.parseInt( vacuumSquareSide.getText() );
                       }catch( Exception e ){
                            JOptionPane.showMessageDialog(null, "Unable to parse square side parameter: \""+vacuumSquareSide.getText()+"\"", "Error", JOptionPane.ERROR_MESSAGE);
                            return;                           
                       }
                       ArrayList<Waypoint> generated_waypoints = WaypointGenerator.vacuumSquare( square_side, speed);
                       Iterator<Waypoint> waypoints_iterator = generated_waypoints.iterator();
                       MapGUI mapGUI = new MapGUI(true,false);
                       mapGUI.setVisible(true);
                       mapGUI.mapImage = new MapImage(true);
                       lastGotoWaypoint=new Waypoint(0,0,0);
                       int waypoints_index = waypoints.size();
                       while( waypoints_iterator.hasNext() ){
                           Waypoint wpt = waypoints_iterator.next();
                           while( !wpt.isOrdinary() && waypoints_iterator.hasNext() ) {
                               wpt = waypoints_iterator.next();
                           }
                           if( !wpt.isOrdinary() ) {
                               break;
                           }
                           mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( wpt.toString() );  
                           mapGUI.mapImage.addWaypoint(wpt);
                           mapGUI.repaint();     
                           goto_waypoint(wpt);                               
                           while( waypoints_index < waypoints.size() ){
                               mapGUI.mapImage.addWaypoint( waypoints.get(waypoints_index++) );
                               mapGUI.repaint();     
                           }
                       }
                       mrs.irobot.baloghkoys.MrsIrobotBaloghkoys.gui.nextWaypointLabel.setText( "finished" );                      
                    }
                }
            );
            goWaypointsThread.start();  
        }else{
            Logger.log("Following waypoints already! Try later");
        }
    }//GEN-LAST:event_vacuumButtonActionPerformed

    private void buttonStopVacuumingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopVacuumingActionPerformed
        goWaypointsThread.interrupt();// este neviem ci to bude dobre
        goWaypointsThread.stop();
        this.lowLevelDrv.stop();
        Logger.log("Vacuum control: Stop"); 
    }//GEN-LAST:event_buttonStopVacuumingActionPerformed

    private void vacuumSquareSideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vacuumSquareSideActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vacuumSquareSideActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JLabel actualOrientationLabel;
    private javax.swing.JLabel actualSpeedLabel;
    private javax.swing.JLabel actualXpositionLabel;
    private javax.swing.JLabel actualYpositionLabel;
    private javax.swing.JButton buttonBackward;
    private javax.swing.JButton buttonBackwardLowLevel;
    private javax.swing.JButton buttonForward;
    private javax.swing.JButton buttonForwardLowLevel;
    private javax.swing.JButton buttonLeft;
    private javax.swing.JButton buttonRight;
    private javax.swing.JButton buttonRotate;
    private javax.swing.JButton buttonStopAutomatic;
    private javax.swing.JButton buttonStopAutomatic1;
    private javax.swing.JButton buttonStopLowLevel;
    private javax.swing.JButton buttonStopManual;
    private javax.swing.JButton buttonStopVacuuming;
    private javax.swing.JButton buttonTurnBack;
    private javax.swing.JButton buttonTurnLeftAngle;
    private javax.swing.JButton buttonTurnLeftAngleLowLevel;
    private javax.swing.JButton buttonTurnRightAngle;
    private javax.swing.JButton buttonTurnRightAngleLowLevel;
    private javax.swing.JButton chooseFileButton;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JLabel drivenDistanceLabel;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    public javax.swing.JTextArea fileNameTextArea;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JRadioButton fromFileRadio;
    private javax.swing.JButton goFileButton;
    private javax.swing.JButton goPointButton;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JInternalFrame jInternalFrame2;
    private javax.swing.JInternalFrame jInternalFrame3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    public static javax.swing.JTextArea logTextArea;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel nextWaypointLabel;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JButton replayFileButton;
    private javax.swing.JButton resetPositionButton;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JButton saveWaypointsButton;
    private javax.swing.JSlider sliderAngle;
    private javax.swing.JSlider sliderDistance;
    private javax.swing.JSlider sliderSpeedAutomatic;
    private javax.swing.JSlider sliderSpeedLowLevel;
    private javax.swing.JSlider sliderSpeedManual;
    private javax.swing.JSlider sliderSpeedVacuum;
    public javax.swing.JLabel timeLabel;
    private javax.swing.JRadioButton toPointRadio;
    private javax.swing.JButton vacuumButton;
    private javax.swing.JRadioButton vacuumSqareRadio;
    private javax.swing.JTextField vacuumSquareSide;
    public javax.swing.JPanel viewPanel;
    private javax.swing.JTextField xTextField;
    private javax.swing.JTextField yTextField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void keyTyped(KeyEvent ke) {
        String keyText = KeyEvent.getKeyText( ke.getKeyCode() );
        //System.err.println("Pressed: "+keyText);
    }

    /**
     * Take only one pressed key at time
     */
    private String buttonPressed = "";
    
    @Override
    public void keyPressed(KeyEvent ke) {
        int speed=0;
        String keyText = KeyEvent.getKeyText( ke.getKeyCode() );
        if( !buttonPressed.isEmpty() )
            return;
        buttonPressed = keyText;
        Logger.log("Button "+keyText+" pressed", 0);
        switch (keyText) {
            case "Up":
                this.buttonForwardLowLevel.doClick();
                break;
            case "Down":
                buttonBackwardLowLevel.doClick();
                break;
            case "Left":  
                buttonTurnLeftAngleLowLevel.doClick();
                break;
            case "Right":
                buttonTurnRightAngleLowLevel.doClick();
                break;
            case "Q":
                speed = this.sliderSpeedLowLevel.getValue();
                speed += 25;          
                if( speed > this.sliderSpeedLowLevel.getMaximum() )
                    speed = this.sliderSpeedLowLevel.getMaximum();
                this.sliderSpeedLowLevel.setValue(speed);
                break;
            case "A":
                speed = this.sliderSpeedLowLevel.getValue();
                speed -= 25;
                if( speed < this.sliderSpeedLowLevel.getMinimum() )
                    speed = this.sliderSpeedLowLevel.getMinimum();
                this.sliderSpeedLowLevel.setValue(speed);
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        String keyText = KeyEvent.getKeyText( ke.getKeyCode() );
        if( !buttonPressed.equals(keyText) )
            return;
        buttonPressed = "";
        Logger.log("Button "+keyText+" released", 0);
        this.lowLevelDrv.stop();            //TODO something more smart
    }
    
    private int lastAngle = 0;                      //TODO znuluj ked sa nuluje vsetko
    private final boolean REAL_POSITION=false;      //TODO CHANGE TO true
    
    private boolean goto_waypoint( Waypoint wpt ){        
        
        double deltaX;
        double deltaY; 
        
       Logger.log(String.format("from x:%4.2f y:%4.2f rot:%4d",this.lowLevelDrv.sensors.get_x_position(),this.lowLevelDrv.sensors.get_y_position(),this.lowLevelDrv.sensors.angle()),3);
       Logger.log(String.format("to   x:   %4d y:   %4d",wpt.getX(),wpt.getY()),3);

       if(REAL_POSITION){
            deltaX =  (this.lowLevelDrv.sensors.get_x_position()-wpt.getX());
            deltaY =  (this.lowLevelDrv.sensors.get_y_position()-wpt.getY());
       }else{
            deltaX = wpt.getX()-lastGotoWaypoint.getX();
            deltaY = wpt.getY()-lastGotoWaypoint.getY();
       }
        
        int distance = (int)Math.sqrt(deltaX*deltaX + deltaY*deltaY);
        double deltaAngle = Math.atan2( deltaY, deltaX )/Math.PI*180.0-90;
        
        if( !(deltaX == 0 && deltaY == 0) ){ 
            lastGotoWaypoint = wpt;
            double rotateAngle = (deltaAngle-lastAngle);
            while( rotateAngle < 180 ) {
                rotateAngle += 360;
            }
            while( rotateAngle > 180 ) {
                rotateAngle -= 360;
            }         
            Logger.log(String.format("Rotating %d Degrees by speed %d mm/s",(int)rotateAngle,wpt.getSpeed()/2),3);
            this.lowLevelDrv.turn( wpt.getSpeed()/2, (int)rotateAngle, waypoints ); 
            Logger.log(String.format("Moving forward %d mm by speed %d mm/s",(int)distance,wpt.getSpeed()),3);
            this.lowLevelDrv.go_forward( wpt.getSpeed(), distance, waypoints );
        }
        Logger.log("Moving to "+wpt.toString()+" completed");
        
        if(REAL_POSITION){
            lastAngle = this.lowLevelDrv.sensors.angle()-90;   //TODO TENTO UHOL TREBA PRISPOSOBIT, LEBO TEN NESEDEL A PRETO TO NESLO!!!  
        }else{
            lastAngle = (int)deltaAngle;             
        }
     
        
        return true;
    }
}
